#include "Server.hpp"

void OnConnected(ConnPtr conn){
    LOG(LogLevel::DEBUG) << "NEW CONNECTION : %s" << conn.get();
}

void OnMessage(ConnPtr conn, Buffer* buf){
    std::string s = buf->ReadAsString(buf->ReadableSize());
    LOG(LogLevel::DEBUG) << s;
    std::string ret = s + " : hello world";
    conn->Send((char*)ret.c_str(), ret.size());
}

void OnClose(ConnPtr conn){
    LOG(LogLevel::DEBUG) << "CLOSE FD: " << conn->Fd();
}

void OnAny(ConnPtr conn){
    LOG(LogLevel::DEBUG) << "OnAny : " << conn->Fd();
}

// ./test_svr.exe server_port
int main(int argc, char* argv[]){
    if(argc != 2) return -1;

    //这里是端口号
    uint16_t server_port = std::stoi(argv[1]);

    TcpServer server(server_port);

    /*给connection设置回调函数*/
    server.SetConnectedCallback(std::bind(OnConnected, std::placeholders::_1));
    server.SetAnyEventCallback(std::bind(OnAny, std::placeholders::_1));
    server.SetMessageCallback(std::bind(OnMessage, std::placeholders::_1, std::placeholders::_2));
    server.SetClosedCallback(std::bind(OnClose, std::placeholders::_1));

    /*设置线程数量*/
    server.SetLoopThreadNum(2);
    /*启动超时任务*/
    server.EnableInactiveRelease(2);
    /*启动*/
    server.Start();

    return 0;
}